#version 300 es
precision highp  float;
in highp vec2 votextture;
out highp vec4 outColor;
uniform sampler2D ourTexture;
highp  float k1 = -0.454201041;
highp float k2 = 0.266614672;
highp  float k3 = -0.0967655739;
highp float p1 = 0.000374852494;
highp float p2 = 0.000804914931;
highp float fx = 909.4744067;
highp float fy = 904.47449409;
highp float cx = 629.98081148;
highp float cy = 383.88105564;
highp float bilinear(highp float in00,highp  float in01,highp  float in10,highp  float in11,highp  float x,highp  float y)
{   highp float tem= in00 + (in01 - in00) * x + (in10 - in00) * y
+ (in11 - in01 - in10 + in00) * x * y;
    return tem;
}
void main() {
//    outColor = texture(texture1,votextture);
    highp vec2 pos = votextture.xy;
    highp vec2 texSize=vec2(720.0,1280.0);
    highp vec2 onePixel = vec2(1, 1) / texSize;
    //图片的坐标位置
    highp vec2 relpos=votextture*texSize;

    highp  float y=(relpos.y-cy)/fy;
    highp float x = (relpos.x - cx) / fx;
    highp float r2 = pow(relpos.x ,2.0)+pow(relpos.y ,2.0);
    highp float scale = 1.0f + k1 * r2 + k2 * pow(r2,2.0) + k3 * pow( r2,3.0f);
    highp float xx = x * scale + 2.0f * p1 * x * y + p2 * (r2 + 2.0f * pow( x,2.0f));
    highp float yy = y * scale + p1 * (r2 + 2.0f * pow( y,2.0f)) + 2.0f * p2 * x * y;
    highp  float uu = (xx * fx + cx);
    highp  float vv = (yy * fy + cy);
    //归一化位置 用来采样器采样
    highp  float nouu=uu;
    highp  float novv=vv;
    highp  vec2 samplePos=(vec2(relpos.x,relpos.y)+vec2(0.0,0.0))*onePixel;
    highp vec2 samplePos1=(vec2(relpos.x,relpos.y)+vec2(1.0,0.0))*onePixel;
    highp vec2 samplePos2=(vec2(relpos.x,relpos.y)+vec2(0.0,1.0))*onePixel;
    highp vec2 samplePos3=(vec2(relpos.x,relpos.y)+vec2(1.0,1.0))*onePixel;
    //还原到正常的颜色  用来线性插值
    highp vec4 sampleColor=texture(ourTexture,samplePos)*255.0;
    highp vec4 sampleColor1=texture(ourTexture,samplePos1)*255.0;
    highp vec4 sampleColor2=texture(ourTexture,samplePos2)*255.0;
    highp vec4 sampleColor3=texture(ourTexture,samplePos3)*255.0;

    highp   float a=bilinear(sampleColor.r,
    sampleColor1.r,sampleColor2.r
    ,sampleColor3.r,fract(nouu),fract(novv));
    ////
    lowp  float r=bilinear(sampleColor.g,
    sampleColor1.g,sampleColor2.g
    ,sampleColor3.g,fract(nouu),fract(novv));
    ////
    lowp  float g=bilinear(sampleColor.b,
    sampleColor1.b,sampleColor2.b
    ,sampleColor3.b,fract(nouu),fract(novv));
    //归一化采样 输出
    outColor=vec4(a/255.0,r/255.0,g/255.0,1.0);

}
